package com.fashion.service.gallery;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import com.fashion.model.gallery.Album;
import com.fashion.utils.Variable;

@Service("albumService")
public class AlbumService { 
    
	@Resource(name = "hibernateTemplate")
	private HibernateTemplate template;
	@Resource(name = "photoService")
    private PhotoService photoService;
	
	public List<Album> findAll(){
		return template.find("from Album order by id");
	}
	
	public List<Album> find(String galleryTitle, String galleryColumn, String order, int offset, int limit) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Album.class);
		if(order != null && !"".equals(order)){
			criteria.addOrder(Order.desc(order));
		}
		criteria.addOrder(Order.desc("date"));
		if(galleryTitle != null && !"".equals(galleryTitle)){
			criteria.add(Restrictions.ilike("title", galleryTitle, MatchMode.ANYWHERE));
		}
		if(galleryColumn != null && !"".equals(galleryColumn)){
			criteria.add(Restrictions.eq("column.id", galleryColumn));
		}
		return template.findByCriteria(criteria, offset, limit);
	}
	
	public int count(String galleryTitle, String galleryColumn) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Album.class);
		if(galleryTitle != null && !"".equals(galleryTitle)){
			criteria.add(Restrictions.like("title", "%" + galleryTitle + "%"));
		}
		if(galleryColumn != null && !"".equals(galleryColumn)){
			criteria.add(Restrictions.eq("column.id", galleryColumn));
		}
		criteria.setProjection(Projections.count("id"));
		return ((Integer)template.findByCriteria(criteria).get(0)).intValue();
	}
	
	public List<Album> findByPriority(Integer priority ){
	    return template.find("from Album where priority = ? order by date desc", priority);
	}
	
	public List<Album> findByKeywords(List<String> keywords, int offset, int limit) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Album.class);
		criteria.addOrder(Order.desc("date"));
		Criterion criterion = null;
		if(keywords != null && keywords.size() > 0){
			int keyNum = 0;
			for(int i = 0; i < keywords.size(); i++){
				String keyword = keywords.get(i);
				if(keyword != null && !"".equals(keyword.trim())){
					Criterion c = Restrictions.ilike("keywords", keyword, MatchMode.ANYWHERE);
					keyNum++;
					if(keyNum == 1){
						criterion = c;
					}else{
						criterion = Restrictions.or(criterion, c);
					}
				}		
			}
			criteria.add(criterion);		
		}	
		return template.findByCriteria(criteria, offset, limit);
	}
	
	public List<Album> findByColumn(List<String> columns, int offset, int limit){
	    if(columns != null && columns.size() != 0){
            DetachedCriteria criteria = DetachedCriteria.forClass(Album.class);
            criteria.add(Restrictions.in("column.id", columns));
            criteria.addOrder(Order.desc("priority"));
            criteria.addOrder(Order.desc("date"));
            List<Album> list = template.findByCriteria(criteria, offset, limit);
    	    return list;
	    }
	    return null;
    }
	
	public int countByColumn(List<String> columns){
	    if(columns != null && columns.size() != 0){
            DetachedCriteria criteria = DetachedCriteria.forClass(Album.class);
            criteria.add(Restrictions.in("column.id", columns));
            criteria.setProjection(Projections.count("id"));
    		return ((Integer)template.findByCriteria(criteria).get(0)).intValue();
	    }
	    return 0;
    }
	
	public List<Album> findByColumn(String column){
		return template.find("from Album where id = ?", column);
	}
	
	public List<Album> findByName(String name){
		return template.find("from Album where name = ?", name);
	}
	
	public Album findById(String id){
	    return template.get(Album.class, id);
	}
	
	public void addAlbum(Album album){
	    album.setId(String.valueOf(System.currentTimeMillis()));
	    album.setDate(new Date());
		template.save(album);
	}
	
	public void delAlbum(Album album){
		template.delete(album);
	}
	
	public void updateAlbum(Album album){
	    album.setDate(new Date());
		template.update(album);
	}
	
	public void click(String albumId){
	    template.bulkUpdate("update Album set clickNumber=clickNumber+1 where id=?", albumId);
	}
	
	public void updateCover(String albumId, String photoName){
	    template.bulkUpdate("update Album set cover=? where id=?", photoName, albumId);
	}
	
	public void saveTransverseCover(File src, String saveName, String saveFolder, boolean autoZoom) throws Exception{
	    String folder = saveFolder + File.separator + Variable.TRANSVERSE_COVER_DIR_SUBSTR;
	    if(autoZoom)
	    	photoService.savePhoto(new FileInputStream(src), saveName, folder, Variable.TRANSVERSE_COVER_SIZE);
	    else
	    	photoService.savePhoto(new FileInputStream(src), saveName, folder, Integer.MAX_VALUE);
	    folder = saveFolder + File.separator + Variable.TRANSVERSE_COVER_DIR_SUBSTR + File.separator + Variable.COVER_MINI_DIR_SUBSTR;

        photoService.savePhoto(new FileInputStream(src), saveName, folder, Variable.TRANSVERSE_COVER_MINI_SIZE);

    }
	
	public void saveLognitudinalCover(File src, String saveName, String saveFolder, boolean autoZoom) throws Exception{
		String folder = saveFolder + File.separator + Variable.LOGNITUDINAL_COVER_DIR_SUBSTR;
        if(autoZoom)
        	photoService.savePhoto(new FileInputStream(src), saveName, folder, Variable.LOGNITUDINAL_COVER_SIZE);
        else
        	photoService.savePhoto(new FileInputStream(src), saveName, folder, Integer.MAX_VALUE);
        folder = saveFolder + File.separator + Variable.LOGNITUDINAL_COVER_DIR_SUBSTR + File.separator + Variable.COVER_MINI_DIR_SUBSTR;
        photoService.savePhoto(new FileInputStream(src), saveName, folder, Variable.LOGNITUDINAL_COVER_MINI_SIZE);

    }
	
}
